package com.itheima.reflect;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
*
* 获取方法并调用
* */

public class Test4Method {

    @Test
    public void testGetMethods() throws Exception {
        Class c1=Student.class;

        //获取类的全部成员方法
        Method[] methods = c1.getDeclaredMethods();
//        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName()+"--->"+method.getParameterCount()+"--->"+method.getReturnType());
        }


        System.out.println("-----------------------------------------------------------------");
        //获取某个方法对象
        Method run = c1.getDeclaredMethod("run");
        System.out.println(run.getName()+"--->"+run.getParameterCount()+"--->"+run.getReturnType());

        //获取有参数的方法对象
        Method eat = c1.getDeclaredMethod("eat",String.class);
        System.out.println(eat.getName()+"--->"+eat.getParameterCount()+"--->"+eat.getReturnType());



        //成员方法的作用： 依然是执行
        Student s=new Student(); //无参数构造...
        run.setAccessible(true);    //禁止检查访问权限
        Object rs = run.invoke(s); //好像运动时间到了 ，rs是run方法的返回值为null
        System.out.println(rs); //

        eat.setAccessible(true);
        String rs2 = (String)eat.invoke(s, "自助牛排");
        System.out.println(rs2); //eat(String name)的返回值


    }
}
